package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Event9ld;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("Event9ldController")
public class Event9ldController extends CmiEmaController {

	@RequestMapping("/listEvent9lds")
	public ModelAndView listEvent9lds(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = event9ldPaginationService.updatePagination(request, pagination);
		Set<Event9ld> event9lds = event9ldPaginationService.getPaginationEvent9lds(pagination);

		mav.addObject("event9lds", event9lds);
		mav.addObject("entityName", "event9ld");
		mav.addObject("pagination", pagination);
		mav.setViewName("event9ld/listEvent9lds");
		return mav;
	}

	public String listEvent9lds() {
		return "redirect:/listEvent9lds";
	}

	@RequestMapping("/viewEvent9ld")
	public ModelAndView viewEvent9ld(@RequestParam Integer event9ldId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("event9ld", event9ldDAO.findEvent9ldByPrimaryKey(event9ldId));
		mav.setViewName("event9ld/viewEvent9ld");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteEvent9lds")
	public ModelAndView confirmDeleteEvent9lds(
			@RequestParam(value = "deleteEvent9ldsIds", required = true) Set<Integer> deleteEvent9ldsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Event9ld> deleteEvent9lds = event9ldDAO.findEvent9ldsByIds(deleteEvent9ldsIds);
		Set<Event9ld> referencedEvent9lds = event9ldService.loadReferenceEvent9lds(deleteEvent9lds);
		boolean hasReferenced;
		if (referencedEvent9lds.size() == 0) {
			mav.addObject("deleteEntitys", deleteEvent9lds);
			hasReferenced = false;
		} else {
			deleteEvent9lds.removeAll(referencedEvent9lds);
			mav.addObject("referencedEntitys", referencedEvent9lds);
			mav.addObject("deleteEntitys", deleteEvent9lds);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Event9ld");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listEvent9lds");
		return mav;
	}

	@RequestMapping("/deleteEvent9ld")
	public String deleteEvent9ld(@RequestParam Integer event9ldId) {
		Event9ld event9ld = event9ldDAO.findEvent9ldByPrimaryKey(event9ldId);
		event9ldService.deleteEvent9ld(event9ld);
		return "forward:/listEvent9lds";
	}

	@RequestMapping("/confirmDeleteEvent9ld")
	public ModelAndView confirmDeleteEvent9ld(@RequestParam Integer event9ldId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("event9ld", event9ldDAO.findEvent9ldByPrimaryKey(event9ldId));

		mav.setViewName("event9ld/deleteEvent9ld");

		return mav;
	}

	@RequestMapping("/deleteEvent9lds")
	public String deleteEvent9lds(
			@RequestParam(value = "deleteEvent9ldsId", required = true) Set<Integer> deleteEvent9ldsId) {
		event9ldService.deleteEvent9lds(deleteEvent9ldsId);
		return "forward:/listEvent9lds";
	}

	@RequestMapping("/editEvent9ld")
	public ModelAndView editEvent9ld(@RequestParam Integer event9ldId) {

		ModelAndView mav = new ModelAndView();

		Event9ld event9ld = event9ldDAO.findEvent9ldByPrimaryKey(event9ldId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("event9ld", event9ld);

		// 把所有的引用event9ld对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(event9ldService.loadEvent9ldObjectReferencedSet(event9ld));

		// 将编辑页面event9ld添加到mav中
		mav.setViewName("event9ld/editEvent9ld");
		return mav;
	}

	@RequestMapping("/newEvent9ld")
	public ModelAndView newEvent9ld() {
		ModelAndView mav = new ModelAndView();
		Event9ld event9ld = new Event9ld();

		mav.addObject("event9ld", event9ld);

		// 把所有的引用event9ld对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(event9ldService.loadEvent9ldObjectReferencedSet(event9ld));

		mav.addObject("newFlag", true);
		mav.setViewName("event9ld/editEvent9ld");

		return mav;
	}

	@RequestMapping("/saveEvent9ld")
	public ModelAndView saveEvent9ld(@ModelAttribute Event9ld event9ld) {

		ModelAndView mav = new ModelAndView();

		Event9ld existingEvent9ld = CmiHibernateProxyUtil
				.deproxy(event9ldDAO.findEvent9ldByPhysicalPrimaryKey(event9ld.getNumber()));

		// 新建记录
		if (event9ld.getId() == null) {
			if (event9ld.equals(existingEvent9ld)) {

				mav.addAllObjects(event9ldService.loadEvent9ldClassReferencedSet(event9ld));
				// mav = editContinue(mav, event9ld);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("event9ld/editEvent9ld");
			} else {
				event9ldService.saveEvent9ld(event9ld);
				mav.setViewName("forward:/listEvent9lds");
			}
		}
		// 编辑记录
		else {
			Event9ld originEvent9ld = CmiHibernateProxyUtil
					.deproxy(event9ldDAO.findEvent9ldByPrimaryKey(event9ld.getId()));
			if (!event9ld.equals(originEvent9ld) && event9ld.equals(existingEvent9ld)) {
				mav.addAllObjects(event9ldService.loadEvent9ldClassReferencedSet(event9ld));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("event9ld/editEvent9ld");
			} else {
				event9ldService.saveEvent9ld(event9ld);
				mav.setViewName("forward:/listEvent9lds");
			}
		}
		return mav;
	}

	@RequestMapping("/event9ldController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
